<?php 
//wsyS Y
function escape1($str) {
  return str_replace('\\', '%', substr(json_encode($str), 1, -1));
}//escape1 网络方案
function unescape1($str) {
  return json_decode('"'.str_replace('%', '\\', $str).'"');
}//unescape1 网络方案
/*PHP 实现js的escape()  */
function wxyS_escape_1($string,$in_encoding ='UTF-8',$out_encoding ='UCS-2') 
{ 
	$jsblstr1 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*+-./@_";
    $return = ''; 
    if(function_exists('mb_get_info')) { 
        for($x=0;$x<mb_strlen($string,$in_encoding);$x++){
			//echo substr($string,$x,1)."</br>";
			if(strpos($jsblstr1,substr($string,$x,1))!=false){
				$return = $return . substr($string,$x,1);
			}else{
				$str = mb_substr($string,$x,1,$in_encoding ); 
				if (strlen($str)>1){//多字节字符 
					$return = $return . '%u'.strtoupper (bin2hex(mb_convert_encoding($str,$out_encoding,$in_encoding))); 
				} else {
					$return = $return .'%'.strtoupper(bin2hex($str)); 
				} //if (strlen
			}//if(strpos($jsblstr1,$str))
        }//for
    }//if
    return $return; 
}//escape
/*PHP 实现js的unescape()  */
function wxyS_unescape_1($str) 
{ 
    $ret = ''; 
    $len = strlen($str); 
    for ($i = 0; $i < $len; $i ++) 
    { 
        if ($str[$i] == '%' && $str[$i + 1] == 'u') 
        { 
            $val = hexdec(substr($str, $i + 2, 4)); 
            if ($val < 0x7f) 
                $ret .= chr($val); 
            else  
                if ($val < 0x800) 
                    $ret .= chr(0xc0 | ($val >> 6)) . 
                     chr(0x80 | ($val & 0x3f)); 
                else 
                    $ret .= chr(0xe0 | ($val >> 12)) . 
                     chr(0x80 | (($val >> 6) & 0x3f)) . 
                     chr(0x80 | ($val & 0x3f)); 
            $i += 5; 
        } else  
            if ($str[$i] == '%') 
            { 
                $ret .= urldecode(substr($str, $i, 3)); 
                $i += 2; 
            } else 
                $ret .= $str[$i]; 
    } 
    return $ret; 
}//unescape
/*PHP 实现js的charCodeAt()  */
//charCodeAt(substr($f, $e, 1))等同于js代码$f.charCodeAt($e)
function charCodeAt($str)//等同于js的charCodeAt()  
{  
    $result = array();  
    for($i = 0,$l = mb_strlen($str,'utf-8');$i<$l;++$i)  
    {  
		$from_encoding = false;
		$Str1 = mb_substr($str, $i, 1, 'utf-8');
		if (strlen($Str1) == 1){
        	$result[] = ord($Str1); 
		}else{
			$Str1 = mb_convert_encoding($Str1,'UCS-4BE',$from_encoding);  
			$tmp = unpack('N',$Str1);  
			$result[] = $tmp[1]; 
		}//if
        //$result[] = uniord(mb_substr($str, $i, 1, 'utf-8'));  
    }  
    return join(",", $result);  
}
/*PHP 实现js的charCodeAt()wei  */
/*PHP 实现js的charCodeAt()  */
function fromCharCode($cs)//等同于js的fromCharCode()  
{ 
	$N_str1 = explode(",",$cs);
	$N_str2 = "";
	$N_JS1 = count($N_str1);
	$i1 = 0;
	while($i1<$N_JS1){
		$N_str2 = $N_str2 . chr($N_str1[$i1]);
		$i1 = $i1 + 1;
	}//while
	return $N_str2;
}//fromCharCode($str)
/*PHP 实现js的fromCharCode()wei  */
/*PHP UT8toUT16和UT16toUT8 */
function wxyS_UT8toUT16($string){
	$string = str_replace('/\r\n/g',"\n",$string);
	$utftext = "";
	//echo $string;
	for($n=0;$n<strlen($string);$n++){
		$c = charCodeAt(substr($string,$n,1));
		if($c<128){
			$utftext = $utftext.fromCharCode(substr($c,0,strlen($c)));
		}else{
			if(($c>127)&&($c<2048)){
				$utftext = $utftext.fromCharCode((substr($c,0,strlen($c))>>6)|192);
				$utftext = $utftext.fromCharCode((substr($c,0,strlen($c))&63)|128);
			}else{							   
				$utftext = $utftext.fromCharCode((substr($c,0,strlen($c))>>12)|224);	
				$utftext = $utftext.fromCharCode(((substr($c,0,strlen($c))>>6)&63)|128);
				$utftext = $utftext.fromCharCode((substr($c,0,strlen($c))&63)|128);	
			}//
		}//
	}//for(var n = 0; n < string.length; n++)
	return $utftext;
}//UT8toUT16
function wxyS_UT16toUT8($utftext){
	$string = "";
	$i = 0;
	$c = 0;
	$c1 = 0;
	$c2 = 0;
	$c3 = 0;
	while($i<strlen($utftext)){
		$c = charCodeAt(substr($utftext,$i,1));
		if($c<128){
			$string = $string . fromCharCode(substr($c,0,strlen($c)));
			$i=$i+1;
		}else{
			if(($c>191)&&($c<224)){
				$c2 = charCodeAt(substr($utftext,($i+1),1));
				$string = $string . fromCharCode(((substr($c,0,strlen($c))&31)<<6)|(substr($c2,0,strlen($c2))&63));							 
				$i=$i+2;
			}else{
				$c2c2 = charCodeAt(substr($utftext,($i+1),1));
				$c3 = charCodeAt(substr($utftext,($i+2),1));
				$string = $string . fromCharCode(((substr($c,0,strlen($c)) & 15) << 12) | ((substr($c2,0,strlen($c2)) & 63) << 6) | (substr($c3,0,strlen($c3)) & 63));		
				$i=$i+3;
			}//if(($c>191)&&($c<224
		}//if($c<128)
	}//while
	return $string;
}//UT16toUT8
class wxyS_Base64{
    private $_base64hash = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; /*这是Base64编码使用的标准字典*/
    private $_DecodeTable = array( /* 这是php源码中使用的解码表,包含了256个字符对应的编码 */
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
        -2,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
        -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
        -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
    );
    private $_encode_data = 0xfc0000;
    private $_debug = false;
    private $_encode_result = '';
    public function encode($str) {
        $len = strlen($str);
        $num = 0;
        $bin = 0;
        $arr = array();
        if($len >= 3) {
            for($i=0;$i<$len;$i++) {
                $bin = $bin << 8;
                if($this->_debug) {
                    echo '$bin = ',decbin($bin),"\n";
                    echo 'binary = ', decbin(ord($str[$i])),"\n";
                }
                $bin = $bin | ord($str{$i});
                if(($i+1)%3 == 0) {
                    $this->_encode_func($bin,3);
                    $bin = 0;
                }
            }
        }
        if($len%3 == 1) {
            $bin = ord($str[$len-1]);
            $bin = $bin << 4;
            $this->_encode_func($bin,1);
            $this->_encode_result .= '==';
        } else if($len%3 == 2) {
            $bin = ord($str[$len-2]);
            $bin = $bin << 8;
            $bin = $bin | ord($str[$len-1]);
            $bin = $bin << 2;
            $this->_encode_func($bin,2);
            $this->_encode_result .= '=';
        }
        return $this->_encode_result;
    }
    private function _encode_func($bin,$bytes = 3) {
        $num = 3;
        $matches = 0;
        $bits1 = ($num - $bytes) * 6;
        $bits2 = $bytes * 6;
        $matches = $this->_encode_data >> $bits1;
        while( $matches ) {
            $result = $bin & $matches;
            $result = $result >> $bits2;
            $bytes--;
            $bits1 = ($num - $bytes) * 6;
            $bits2 = $bytes * 6;
            $matches = $this->_encode_data >> $bits1;
            if($this->_debug) {
                echo '$result = ',$result,' binary = ',decbin($result),"\n";
            }
            $this->_encode_result .= $this->_base64hash[$result];
        }
    }
    public function decode($str) {
        $bin = 0;
        $length = strlen($str)-1;
        $_decode_result = '';
        $len = 0;
        $i = 0;
        while(($len <= $length)){
            $ch = $str[$len++];
            if ($ch == '=') { // 当前一个字符是“=”号
                /*
                    先说明一个概念：在解码时，4个字符为一组进行一轮字符匹配。
                    如果某一轮匹配的第二个是“=”且第三个字符不是“=”，说明这个带解析字符串不合法，直接返回空
                */
                if($str[$len-1] != '=' && (($i % 4) == 1)) {
                    return NULL;
                }
                continue;
            }
            $ch = $this->_DecodeTable[ord($ch)];
            // 下面这连个条件，只有 ch < 0 会起作用，ch == -2 永远不会起作用，即所有不合法的字符均跳过。
            if ($ch < 0 || $ch == -1) { /* a space or some other separator character, we simply skip over */
                continue;
            } else if ($ch == -2) {
                return NULL;
            }
            switch($i % 4) {
                case 0:
                    $bin = intval($ch) << 2;
                    break;
                case 1:
                    $bin = intval($bin) | intval($ch) >> 4;
                    $_decode_result .= chr($bin);
                    $bin = ( intval($ch) & 0x0f ) << 4;
                    break;
                case 2:
                    $bin = intval($bin) | intval($ch) >> 2;
                    $_decode_result .= chr($bin);
                    $bin = ( intval($ch) & 0x03 ) << 6;
                    break;
                case 3:
                    $bin = intval($bin) | intval($ch);
                    $_decode_result .= chr($bin);
                    break;
            }
            $i++;
        }
        return $_decode_result;
    }
    public function debug($open = true) {
        $this->_debug = $open;
    }
}//wxyS_Base64
/*
$basesl1 = new wxyS_Base64();
$basesl1->encode("");//E
$basesl1->decode("");//D
*/
//实施base64编码 wxyS方案1
function wxyS_Encode_1($input){
	$N_Str1 = $input;
	$N_Str1 = wxyS_escape_1($N_Str1);
	$N_Str1 = wxyS_UT8toUT16($N_Str1);
	$basesl1 = new wxyS_Base64();
	$N_Str1 = $basesl1->encode($N_Str1);//E
	return $N_Str1;
}//wxyS_Encode_1
//实施base64解码 wxyS方案1
function wxyS_Decode_1($input){
	$N_Str1 = $input;
	$basesl1 = new wxyS_Base64();
	$N_Str1 = $basesl1->decode($N_Str1);//D
	$N_Str1 = wxyS_UT16toUT8($N_Str1);
	$N_Str1 = wxyS_unescape_1($N_Str1);
	return $N_Str1;
}//wxyS_Decode_1

//生成随机字符方案1
function wxyS_RandFA1($str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",$length=1){
	$len=strlen($str)-1;
	$randstr='';
	for($i=0;$i<$length;$i++){
		$num=mt_rand(0,$len);
		$randstr .= $str[$num];
	}//
	return $randstr;
}//wxyS_RandFA1
//实施$Strsl1=wxyS_RandFA1('ABCDEFGHIJKLMNOPQRSTUVWYZ',4);

//生成随机字符整数字符方案1
function wxyS_RandIntFA1($length=1){
	$nNum1 = "";
	$nRandi1=1;
	$nRandi11=$length;
	while($nRandi1<=$nRandi11){
		$nNum1 = $nNum1 . (String)rand(0,9);
		$nRandi1 = $nRandi1 + 1;
	}//
	return $nNum1;
}//wxyS_RandIntFA1
//实施$Strsl1=wxyS_RandIntFA1(4);





?>
<?php //待研发
/*
function wxy_Encode($input) {
	$wxyB64_St1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	$N_Str1 = $input;
	$output = "";$chr1;$chr2;$chr3;$enc1;$enc2;$enc3;$enc4;$i=0;
	$input = escape($input);
	$input = UT8toUT16($input);
	while($i<strlen($input)){
		$chr1 = charCodeAt(substr($input,($i++),1));
		$chr2 = charCodeAt(substr($input,($i++),1));
		$chr3 = charCodeAt(substr($input,($i++),1));
		$enc1 = $chr1>>2;
		$enc2 = ((intval($chr1)&3)<<4)|(intval($chr2)>>4);
		$enc3 = ((intval($chr2) & 15) << 2) | (intval($chr3) >> 6);
		$enc4 = intval($chr3) & 63;
		if(!is_int($chr2)){
			$enc3 = 64; 
			$enc4 = 64;
		}else{ 
			if(!is_int($chr3)){
				$enc4 = 64;
			}
		}
		$output = $output . substr($wxyB64_St1,$enc1,1). substr($wxyB64_St1,$enc2,1). substr($wxyB64_St1,$enc3,1). substr($wxyB64_St1,$enc4,1);
	}
	return $output;
}//wxy_Encode
*/
?>